﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
using System.Text;
using System.IO;
public partial class Admin_NewsUpdate : System.Web.UI.Page
{
   
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
          int  NID = int.Parse(Request.QueryString["id"].ToString());
            Bind(NID);


        }

    }
    private void Bind(int nid)
    {
        News GetN=new News();
        OleDbDataReader dr = GetN.GetNewsConn(nid);
        if (dr.Read())
        {
            ntitle.Text = dr["Ntitle"].ToString();
            textTit.Text = dr["Ntitle2"].ToString();
            TextKey.Text = dr["Nkey"].ToString();
            ntro.Text = dr["description"].ToString();
            ncontent.Value = dr["Ncon"].ToString();
            TextBox1.Text = dr["Htmlurl"].ToString();
        }
        dr.Close();
        dr.Dispose();
 
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string Title1 = ntitle.Text;
        string Title2 = textTit.Text;
        string Key = TextKey.Text;
        string des = ntro.Text;
        string conn = ncontent.Value;
        News Addn = new News();
        //if (Keybool.Checked)
        //{
        //    conn = SetConnLink.SetLink(conn);
        //}
        Addn.UPPNews(Title1, Title2, Key, des, conn, int.Parse(Request.QueryString["id"].ToString()));
        //
        if (Keybool.Checked)
        {
            conn = SetConnLink.GetNoneLink(conn);
            conn = SetConnLink.FormatBody(conn);
        }
        int NID = int.Parse(Request.QueryString["id"].ToString());
        string Toplianjie = WebSet.GetSingleValue("顶部连接");
        string hongxingwangzhan = WebSet.GetSingleValue("红星网站导航");
        string ProList = WebSet.GetProList2();
        string DibuLianjie = WebSet.GetSingleValue("底部版权");

        string danqiandizhi = TextBox1.Text.Trim();
        string yuming = WebSet.GetSingleValue("网站域名");
        conn = conn + "<br/>本文地址：<a href=\"" + yuming + "/" + danqiandizhi + "\">" + yuming + "/" + danqiandizhi + "</a>";
        Title2 += WebSet.GetSingleValue("WebTitle").ToString();
        //生成页面
        try
        {
            StringBuilder strhtml = new StringBuilder();
            //创建StreamReader对象
            using (StreamReader sr = new StreamReader(System.Web.HttpContext.Current.Server.MapPath("~/") + "newsMoBan.html", Encoding.GetEncoding("gb2312")))
            {
                String oneline;
                //读取指定的HTML文件模板
                while ((oneline = sr.ReadLine()) != null)
                {
                    strhtml.Append(oneline);
                }
                sr.Close();
            }
            string FilePath = "";
            strhtml = strhtml.Replace("$Title2", Title2);
            strhtml = strhtml.Replace("$keywords", Key);
            strhtml = strhtml.Replace("$description", des);
            strhtml = strhtml.Replace("$Ntitle1", Title1);
            strhtml = strhtml.Replace("$conn", conn);
            strhtml = strhtml.Replace("$dangqiandizhi", danqiandizhi);
            strhtml = strhtml.Replace("$Toplianjie", Toplianjie);
            strhtml = strhtml.Replace("$hongxingwangzhan", hongxingwangzhan);
            strhtml = strhtml.Replace("$ProList", ProList);
            strhtml = strhtml.Replace("$DibuLianjie", DibuLianjie);
            //strhtml = strhtml.Replace("$maodian", Maodian);


            FilePath = System.Web.HttpContext.Current.Server.MapPath("~/") + danqiandizhi;
            //删除原有文件
            FileInfo file = new FileInfo(FilePath);
            if (file.Exists)
            {
                file.Delete();
            }
            //创建文件信息对象--------------------------------------------
            FileInfo finfo = new FileInfo(FilePath);
            //以打开或者写入的形式创建文件流
            using (FileStream fs = finfo.OpenWrite())
            {
                //根据上面创建的文件流创建写数据流
                StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.Default);
                //把新的内容写到创建的HTML页面中
                sw.WriteLine(strhtml);
                sw.Flush();
                sw.Close();
            }
        }
        catch (Exception err)
        {
            //输出异常信息
            throw new Exception(err.Message, err);
        }



        Response.Write("<script>alert('" + "添加修改成功！" + "');window.location.href=window.location.href;</script>");
    }

}
